<?php


abstract class CartWS_Model_FormulaEngine_Formula_Abstract {
    
    const RET_STOP = CartWS_Model_FormulaEngine_Engine::RET_STOP;
    const RET_TYPE_STOP = CartWS_Model_FormulaEngine_Engine::RET_TYPE_STOP;
    const RET_CONTINUE = CartWS_Model_FormulaEngine_Engine::RET_CONTINUE;

    protected $_engine = null;
    protected $_formula = null;
    protected $_amounts = null;
    
    function __construct( $engine, $formula, &$amounts ) {
        $this->_engine = $engine;
        $this->_formula = $formula;
        $this->_amounts = &$amounts;
    }


    protected function getOrder() {
        return $this->_engine->getCurrentOrder();
    }

    protected function getOrderProperties() {
        return $this->_engine->getCurrentOrderProperties();
    }

    protected function getOrderLines() {
        return $this->_engine->getCurrentOrderLines();
    }

    protected function getOrderLineProperties( $line ) {
        return $this->_engine->getOrderLineProperties( $line );
    }

    protected function getDbTable( $class ) {
        return $this->_engine->getDbTable( $class );
    }

    protected function getDb() {
        return $this->_engine->getDbAdapter();
    }

    protected function getProperties() {
        return $this->_formula->getProperties();
    }

    protected function getType() {
        return $this->_formula->type;
    }

    protected function getCode() {
        return $this->_formula->code;
    }

    protected function getMyAmount() {
        return $this->_amounts[ $this->getType() ][ $this->getCode() ];
    }

    protected function setMyAmount( $amount ) {
        $this->_amounts[ $this->getType() ][ $this->getCode() ] = $amount;
    }

    protected function getMyTypeAmount() {
        $amount = 0;
        return array_sum( $this->_amounts[ $this->getType() ] );
    }

    protected function setMyTypeAmount( $amount ) {
        $this->_amounts[ $this->getType() ] = array();
        $this->_amounts[ $this->getType() ][ $this->getCode() ] = $amount;
    }

    protected function clearMyAmount() {
        unset( $this->_amounts[ $this->getType() ][ $this->getCode() ]);
    }

    protected function clearMyTypeAmount() {
            unset( $this->_amounts[ $this->getType() ]);
    }
    /**
     * This is the main function to calculate discounts, shipping costs or charges added to an order.
     *
     * @param array $amounts an array indexed by type, code of amount calculated til now, can be modified by this function.
     * @return boolean continue or not.
     */
    abstract protected function execute();
}
